//
//  MarketTableViewCell.swift
//  Exchange
//
//  Created by rui on 2018/5/3.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class MarketTableViewCell: UITableViewCell {
    @IBOutlet weak var firstVavityName: UILabel!
    @IBOutlet weak var lastVavityName: UILabel!
    @IBOutlet weak var lastPrice: UILabel!
    @IBOutlet weak var exchangeLastPrice: UILabel!
    @IBOutlet weak var increase: UILabel!
    @IBOutlet weak var hourIncrease: UILabel!
    
    var model: MarketCoinModel? {
        didSet {
            self.configer()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        increase.layer.cornerRadius = 3
        increase.layer.masksToBounds = true
        // Initialization code
        let view = UIView(frame: self.bounds)
        view.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        self.selectedBackgroundView = UIView(frame: self.bounds)
        self.selectedBackgroundView?.themeBackground = ThemeManager.ThemeColorKeys.selectTabColor
    }
    
    
    private func configer() {
        guard let model = self.model else {
            return
        }
        let strs = model.name.components(separatedBy: "/")
        if let first = strs.first {
            self.firstVavityName.text = first
        }
        if let last = strs.last {
            self.lastVavityName.text = "/" + last
        }
        let price = CYCalculate.stringWithNum(num: NSNumber(value: model.price))
        self.lastPrice.text = price
        self.hourIncrease.text = "24H \(Int(model.vol))"
        self.increase.backgroundColor = kGreenColor
        if model.price > 0 {
            let open = model.open == 0 ? 1 : model.open
            let increase = (model.price - model.open) * 100 / open
            if increase >= 0 {
                self.increase.text = String(format: "+%0.2f%%", increase)
                self.increase.backgroundColor = kGreenColor
            }else {
                self.increase.text = String(format: "%0.2f%%", increase)
                self.increase.backgroundColor = kRedColor
            }
        }
        let money = String(format: "%.2f", model.money)
        if AppCacheInfo.default.currency == "USD" {
            self.exchangeLastPrice.text = "$ \(money)"
        }else {
            self.exchangeLastPrice.text = "￥ \(money)"
        }
    }
    
  
    
}
